package com.example.demo.controller;

import com.example.demo.myException.MyDefineException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExceptionController {

    @RequestMapping("/notexception")
    public String getNotException() {
        return "success";
    }

    /**
     * 其他异常
     *
     * @return
     */
    @RequestMapping("/getexception")
    public String getException() {
        int num = 1 / 0;
        return "";
    }

    /**
     * 空指针异常
     *
     * @return
     */
    @RequestMapping("/getNullPointerException")
    public String getNullPointerException() {
        String str = null;
        int strLength = str.length();
        return "";
    }

    /**
     * 会被自己定义的全局异常捕获，并是自己定义的异常类
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("/getMyDefineException")
    public String getMyDefineException() throws Exception {
        try {
            int num = 1 / 0;
        } catch (Exception e) {
            throw new MyDefineException(e.getMessage());
        }
        return "";

    }
}
